#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
#define Color 1
#define Dimming 2
/* widgets:
Layer_CCTLight
Background_CCTLight
Container_CCT_On
Container_TGD_Text
Text_TGD_Baifenhao
Text_TGD_Liangdu
Container_CCT_Button
PopupButton_CCT_OFF
Meter_CCT
Icon_CCT_Pointer
CircleProgressBar_CCT_Light
Text_CCT_KNum
Container_CCT_Off
Text_CCT_OFF
PopupButton_CCT_ON
Icon_CCT_JindutiaoOff
Button_Back_MainMenu
*/
//控件
static ITUBackground* Background_CCTLight;
static ITUMeter* Meter_CCT_Color;
static ITUMeter* Meter_CCT_Dimming;
static ITUContainer* Container_CCT_Off;
static ITUContainer* Container_CCT_On;
static ITUText* Text_CCT_Baifenhao, *Text_CCT_Baifenhao1, *Text_CCT_Color;
static ITUText* Text_CCT_Sewen, *Text_CCT_Liangdu;
static uint8_t CCT_Display_Color_Or_Dimming = Color;
bool CCTLightOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_CCTLight)//首次加载
	{
		Background_CCTLight = ituSceneFindWidget(&theScene, "Background_CCTLight");
		assert(Background_CCTLight);
		Container_CCT_Off = ituSceneFindWidget(&theScene, "Container_CCT_Off");
		assert(Container_CCT_Off);
		Container_CCT_On = ituSceneFindWidget(&theScene, "Container_CCT_On");
		assert(Container_CCT_On);
		Meter_CCT_Color = ituSceneFindWidget(&theScene, "Meter_CCT_Color");
		assert(Meter_CCT_Color);
		Meter_CCT_Dimming = ituSceneFindWidget(&theScene, "Meter_CCT_Dimming");
		assert(Meter_CCT_Dimming);
		Text_CCT_Baifenhao = ituSceneFindWidget(&theScene, "Text_CCT_Baifenhao");
		assert(Text_CCT_Baifenhao);
		Text_CCT_Baifenhao1 = ituSceneFindWidget(&theScene, "Text_CCT_Baifenhao1");
		assert(Text_CCT_Baifenhao1);
		Text_CCT_Color = ituSceneFindWidget(&theScene, "Text_CCT_Color");
		assert(Text_CCT_Color);
		Text_CCT_Sewen = ituSceneFindWidget(&theScene, "Text_CCT_Sewen");
		assert(Text_CCT_Sewen);
		Text_CCT_Liangdu = ituSceneFindWidget(&theScene, "Text_CCT_Liangdu");
		assert(Text_CCT_Liangdu);
	}
	//更具参数初始化界面
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//打开
	{
		ituWidgetSetVisible(Container_CCT_Off, false);
		ituWidgetSetVisible(Container_CCT_On, true);
	}
	else//关闭
	{
		ituWidgetSetVisible(Container_CCT_On, false);
		ituWidgetSetVisible(Container_CCT_Off, true);	
	}
	ituMeterSetValue(Meter_CCT_Dimming, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0]);
	ituMeterSetValue(Meter_CCT_Color, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1]);

	//Text操作
	char TextString[20];
	uint16_t LightValue;
	LightValue = TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1];
	itoa(LightValue, TextString, 10);//转换成字符串
	ituTextSetString(Text_CCT_Color, TextString);//显示字符
	//对“%”坐标位子
	if (Meter_CCT_Color->value >= 100)
		ituWidgetSetX(Text_CCT_Baifenhao, 141);
	else
		ituWidgetSetX(Text_CCT_Baifenhao, 123);
	//对“%”坐标位子
	if (Meter_CCT_Dimming->value >= 100)
		ituWidgetSetX(Text_CCT_Baifenhao1, 141);
	else
		ituWidgetSetX(Text_CCT_Baifenhao1, 123);

	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_CCT_Sewen->fontIndex = 0;
		Text_CCT_Sewen->boldSize = 1;
		ituTextSetFontSize(Text_CCT_Sewen, 18);
		Text_CCT_Liangdu->fontIndex = 0;
		Text_CCT_Liangdu->boldSize = 1;
		ituTextSetFontSize(Text_CCT_Liangdu, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_CCT_Sewen->fontIndex = 8;
		Text_CCT_Sewen->boldSize = 0;
		ituTextSetFontSize(Text_CCT_Sewen, 16);
		Text_CCT_Liangdu->fontIndex = 8;
		Text_CCT_Liangdu->boldSize = 0;
		ituTextSetFontSize(Text_CCT_Liangdu, 16);
	}
	Set_Rotate_Block(30);//旋转步进
    return true;
}

bool CCTLightOnLeave(ITUWidget* widget, char* param)
{
    return true;
}

bool CCTLightOnTimer(ITUWidget* widget, char* param)
{
	static int MeterValue_SW = 0, MeterValue_SW_L = 0;
	static uint32_t SendEndTime_SW = Rotate_SendCmd_Time;
	static int MeterValue_LD = 0, MeterValue_LD_L = 0;
	static uint32_t SendEndTime_LD = Rotate_SendCmd_Time;
	static uint32_t SendTime = 0;
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	if (CCT_Display_Color_Or_Dimming == Color)
	{
		//设备状态执行
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
		{
			if (Action_Cmd.User_Action == Clockwise_Rotation && MeterValue_SW < 100)//顺时针旋转
			{
				MeterValue_SW += Action_Cmd.Action_Times;
				if (MeterValue_SW > 100)MeterValue_SW = 100;
				ituMeterSetValue(Meter_CCT_Color, MeterValue_SW);//设置Meter百分比值
				MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
				Clear_User_Action();
			}
			else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterValue_SW > 0)//逆时针旋转
			{

				MeterValue_SW -= Action_Cmd.Action_Times;
				if (MeterValue_SW < 0)MeterValue_SW = 0;
				ituMeterSetValue(Meter_CCT_Color, MeterValue_SW);//设置Meter百分比值
				MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
				Clear_User_Action();
			}
		}
	}
	else if (CCT_Display_Color_Or_Dimming == Dimming)
	{
		//设备状态执行
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
		{
			if (Action_Cmd.User_Action == Clockwise_Rotation && MeterValue_LD < 100)//顺时针旋转
			{
				MeterValue_LD += Action_Cmd.Action_Times;
				if (MeterValue_LD > 100)MeterValue_LD = 100;
				ituMeterSetValue(Meter_CCT_Dimming, MeterValue_LD);//设置Meter百分比值
				MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
				Clear_User_Action();
			}
			else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterValue_LD > 0)//逆时针旋转
			{

				MeterValue_LD -= Action_Cmd.Action_Times;
				if (MeterValue_LD < 0)MeterValue_LD = 0;
				ituMeterSetValue(Meter_CCT_Dimming, MeterValue_LD);//设置Meter百分比值
				MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
				Clear_User_Action();
			}
		}
		//设备操作
		MeterValue_LD = Meter_CCT_Dimming->value;
	}
	//设备操作
	MeterValue_SW = Meter_CCT_Color->value;
	//色温调节485执行
	if (MeterValue_SW == MeterValue_SW_L)//判断是否停止操作
	{
		if (SendEndTime_SW <= Rotate_SendCmd_Time)
			SendEndTime_SW++;
		//停止操作0.6s后发送执行报文
		if (SendEndTime_SW == Rotate_SendCmd_Time)
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
		}
	}
	else
	{
		SendEndTime_SW = 0;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = MeterValue_SW;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0x0D;//命令调光
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = MeterValue_SW;
	}
	//亮度调节485执行
	if (MeterValue_LD == MeterValue_LD_L)//判断是否停止操作
	{
		if (SendEndTime_LD <= Rotate_SendCmd_Time)
			SendEndTime_LD++;
		//停止操作0.6s后发送执行报文
		if (SendEndTime_LD == Rotate_SendCmd_Time)
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
		}
	}
	else
	{
		SendEndTime_LD = 0;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = MeterValue_LD;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0x03;//命令调光
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = MeterValue_LD;
	}

	//用MSG_GET_DEVICE_STATE_EN标记来表示当前是否在旋转动作中
	if (MessageFlag[MSG_GET_DEVICE_STATE_EN] == OFF)
	{
		if (SendTime < Rotating_SendCmd_Time)
			SendTime++;
		if (SendTime == Rotating_SendCmd_Time)//0.495s发送一次命令
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
		}
	}

	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
		{
			ituWidgetSetVisible(Container_CCT_Off, false);
			ituWidgetSetVisible(Container_CCT_On, true);
			ituMeterSetValue(Meter_CCT_Dimming, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0]);//亮度
			ituMeterSetValue(Meter_CCT_Color, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1]);//色温
		}
		else//关闭
		{
			ituWidgetSetVisible(Container_CCT_On, false);
			ituWidgetSetVisible(Container_CCT_Off, true);
			ituMeterSetValue(Meter_CCT_Dimming, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0]);//亮度
			ituMeterSetValue(Meter_CCT_Color, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1]);//色温
		}

		MeterValue_LD = Meter_CCT_Dimming->value;//防止设备循环上报
		MeterValue_SW = Meter_CCT_Color->value;//防止设备循环上报
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterValue_SW_L = MeterValue_SW;
	MeterValue_LD_L = MeterValue_LD;

	//Text操作
	char TextString[20];
	uint16_t LightValue;
	LightValue = Meter_CCT_Color->value;
	itoa(LightValue, TextString, 10);//转换成字符串
	ituTextSetString(Text_CCT_Color, TextString);//显示字符
	//对“%”坐标位子
	if (Meter_CCT_Color->value >= 100)
		ituWidgetSetX(Text_CCT_Baifenhao, 141);
	else
		ituWidgetSetX(Text_CCT_Baifenhao, 123);
	//对“%”坐标位子
	if (Meter_CCT_Dimming->value >= 100)
		ituWidgetSetX(Text_CCT_Baifenhao1, 141);
	else
		ituWidgetSetX(Text_CCT_Baifenhao1, 123);

    return true;
}

bool PopupButton_CCT_OFF_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0;//状态关
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//命令关
	printf("Moorgen INFO : Touch to turn off the CCT_Light\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_CCT_ON_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//状态开
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 2;//命令开
	printf("Moorgen INFO : Touch to turn on the CCT_Light\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_CCT_Color_Unpitch_Pressed(ITUWidget* widget, char* param)
{
	CCT_Display_Color_Or_Dimming = Color;
	return true;
}

bool PopupButton_CCT_Dimming_Unpitch_Pressed(ITUWidget* widget, char* param)
{
	CCT_Display_Color_Or_Dimming = Dimming;
	return true;
}